#!/bin/sh
#作者：高伟
#my email: xiyou.gaowei@gmail.com
#version: IPv6G 0.1

#用法说明
usage()
{
	echo "Today's date is `date`"
	echo "The software's usage"
	echo "./IPv6G IPv6"
}

#IPv6前缀产生的策略的选择
ipv6()
{
	echo "请选择策略，A或者B，首选为B"
	read scheme

	case "$scheme" in
		A | a) A;;
		B | b) B;;
		*) B;;
	esac

}

#IPv6前缀产生B策略
B() 
{
echo "首选B策略"
echo "ASN+IPv4前缀+随机数"
o=0

echo "请输入一个IPv4前缀,如202.117.128.0/20"
#IPv6前缀长度等于IPv4长度的2倍
read IPv4P

#把IPv4前缀分割成5个部分
ip1=`echo "$IPv4P" | cut -f 1 -d "."`
ip2=`echo "$IPv4P" | cut -f 2 -d "."`
ip3=`echo "$IPv4P" | cut -f 3 -d "."`
ip5=`echo "$IPv4P" | cut -f 4 -d "."`
ip4=`echo "$ip5" | cut -f 1 -d "/"`
IPv4L=`echo "$ip5" | cut -f 2 -d "/"`

#把上面的5个部分，从10进制转化为2进制
IP1=`echo "obase=2; ibase=10; $ip1"|bc`
IP2=`echo "obase=2; ibase=10; $ip2"|bc`
IP3=`echo "obase=2; ibase=10; $ip3"|bc`
IP4=`echo "obase=2; ibase=10; $ip4"|bc`

P=($IP1 $IP2 $IP3 $IP4)
p=(i1 i2 i3 i4)

IPv6L=`expr $IPv4L "*" 2`

#是否需要随机值
if [ "$IPv6L" -gt "48" ]; then
	randomL=`expr $IPv6L - 48`
	echo "自动或手动输入一个 $randomL bit的随机值，自动则输入a即可"
	read random
	#生成一个随机数
	if [ "$random" = "a" ]; then
		flag=r
		r_a=(0 1)
		random=`for ((i=0;i<$randomL;i++)); do echo -n ${r_a[$RANDOM % ${#r_a[*]}]}; done`
	else 
		flag=r
	fi
fi

#把IPv4前缀转换为一个2进制的字符串
for ((i=0;i<4;i++));do
        l=${#P[i]}
        if [ "$l" -le "8" ]; then
                zr_number=`expr 8 - $l`
                a=${P[i]}
                while [ "$zr_number" -gt "$o" ]; do
                        a=0$a
                        zr_number=`expr $zr_number - 1`
                done
        fi
        p[i]=$a
done

#把IPv4前缀转换成俩组16进制的数
i1=`echo "${p[0]}"`
i2=`echo "${p[1]}"`
i3=`echo "${p[2]}"`
i4=`echo "${p[3]}"`
IPv4P11=`echo $i1|awk '{print substr($i1,1,4)}'`
IPv4P12=`echo $i1|awk '{print substr($i1,5,4)}'`
IPv4O11=`echo "obase=16; ibase=2; $IPv4P11"|bc`
IPv4O12=`echo "obase=16; ibase=2; $IPv4P12"|bc`

IPv4P21=`echo $i2|awk '{print substr($i2,1,4)}'`
IPv4P22=`echo $i2|awk '{print substr($i2,5,4)}'`
IPv4O21=`echo "obase=16; ibase=2; $IPv4P21"|bc`
IPv4O22=`echo "obase=16; ibase=2; $IPv4P22"|bc`

IPv4P31=`echo $i3|awk '{print substr($i3,1,4)}'`
IPv4P32=`echo $i3|awk '{print substr($i3,5,4)}'`
IPv4O31=`echo "obase=16; ibase=2; $IPv4P31"|bc`
IPv4O32=`echo "obase=16; ibase=2; $IPv4P32"|bc`

IPv4P41=`echo $i4|awk '{print substr($i4,1,4)}'`
IPv4P42=`echo $i4|awk '{print substr($i4,5,4)}'`
IPv4O41=`echo "obase=16; ibase=2; $IPv4P41"|bc`
IPv4O42=`echo "obase=16; ibase=2; $IPv4P42"|bc`

IPv6P1=`echo "$IPv4O11$IPv4O12$IPv4O21$IPv4O22"`
IPv6P2=`echo "$IPv4O31$IPv4O32$IPv4O41$IPv4O42"`

IPv4_prefix=`echo "$IPv6P1:$IPv6P2"`
#IPv4_prefix=`echo "${p[0]}${p[1]}${p[2]}${p[3]}"`

#判断是否输出随机数
if [ "$flag" = "r" ]; then
#功能上和下面的相同不知道是否可以呢？可以，但是想如何可以合并在一起呢？
	ran=$random
        if [ "$randomL" -lt "16" ]
        then
                z_number=`expr 16 - $randomL`
                while [ "$z_number" -gt "$o" ]; do
                        ran=$ran"0"
                        z_number=`expr $z_number - 1`
                done
        fi
	random1=`echo $ran|awk '{print substr($ran,1,4)}'`
	random2=`echo $ran|awk '{print substr($ran,5,4)}'`
	random3=`echo $ran|awk '{print substr($ran,9,4)}'`
	random4=`echo $ran|awk '{print substr($ran,13,4)}'`
#把随机数转换成16进制的
	orandom1=`echo "obase=16; ibase=2; $random1"|bc`
	orandom2=`echo "obase=16; ibase=2; $random2"|bc`
	orandom3=`echo "obase=16; ibase=2; $random3"|bc`
	orandom4=`echo "obase=16; ibase=2; $random4"|bc`

	orandom=`echo "$orandom1$orandom2$orandom3$orandom4"`
	prefix_random=`echo "$IPv4_prefix:$orandom"`
else
	zero=0000
	prefix_random=`echo "$IPv4_prefix:$zero"`
fi

#创建文件将用于保存输出结果
if [ -e IPv6_Prefix_Btable ]; then
	echo "已经有IPv6_Prefix_Btable"
        echo "是否删除原来的IPv6_Prefix_Btable，yes或者no"
	read y_or_n
	case "$y_or_n" in
		y*) rm IPv6_Prefix_Btable
		    echo "已经删除IPv6_Prefix_Btable";;
		n*) echo "no";;
	esac
else
	touch IPv6_Prefix_Btable
fi

#实现asn的值
asn=`expr 1024 "*" 16 - 1`
while [ "$asn" -ge "$o" ]; do
s=$asn
i=`echo "obase=2; $asn" | bc`
iL=`echo $i|awk '{print length($i)}'`

        if [ "$iL" -lt "16" ]
        then
                z_number=`expr 16 - $iL`
                j=$i
                while [ "$z_number" -gt "$o" ]; do
                        j=0$j
                        z_number=`expr $z_number - 1`
                done
        fi

jL=`echo $j|awk '{print length($j)}'`

L1=`echo $j|awk '{print substr($j,1,4)}'`
L2=`echo $j|awk '{print substr($j,5,4)}'`
L3=`echo $j|awk '{print substr($j,9,4)}'`
L4=`echo $j|awk '{print substr($j,13,4)}'`

l1=`echo "obase=16; ibase=2; $L1"|bc`
l2=`echo "obase=16; ibase=2; $L2"|bc`
l3=`echo "obase=16; ibase=2; $L3"|bc`
l4=`echo "obase=16; ibase=2; $L4"|bc`

ASN=`echo "$l1$l2$l3$l4"`
asn=`expr $s - 1`

#按照IPv6地址规则输出
IPv6P=`echo "$ASN:$prefix_random/$IPv6L"`
IPv6Pa=`echo "$IPv6P" | cut -f 1 -d "/"`
IPv6Pa1=`echo "$IPv6Pa" | cut -f 1 -d ":"`
IPv6Pa2=`echo "$IPv6Pa" | cut -f 2 -d ":"`
IPv6Pa3=`echo "$IPv6Pa" | cut -f 3 -d ":"`
IPv6Pa4=`echo "$IPv6Pa" | cut -f 4 -d ":"`
#IPv6a1,在此次算法中不可以为0，所以不作判断
if [ "$IPv6Pa2" = "0000" ] && [ "$IPv6Pa3" = "0000" ] && [ "$IPv6Pa4" = "0000" ]; then
	IPv6P=`echo "$IPv6Pa1::/$IPv6L"`
fi
if [ "$IPv6Pa2" = "0000" ] && [ "$IPv6Pa3" = "0000" ] && [ "$IPv6Pa4" != "0000" ]; then
	IPv6P=`echo "$IPv6Pa1::$IPv6Pa4/$IPv6L"`
fi
if [ "$IPv6Pa2" != "0000" ] && [ "$IPv6Pa3" = "0000" ] && [ "$IPv6Pa4" = "0000" ]; then
	IPv6P=`echo "$IPv6Pa1:$IPv6Pa2::/$IPv6L"`
fi

#echo "$ASN:$prefix_random/$IPv6L"
echo "$IPv6P"
echo -e "$IPv6P" >> IPv6_Prefix_Btable
done	
}

#IPv6前缀产生的A策略
A()
{
	echo "A group"
}

#目前不知道如何写
ipv4()
{
	echo "IPv4 perfix"
#创建存储表的文件
	if [ -e yiyi ]; then
		echo "已经有yiyi"
		echo "是否删除原来的yiyi，yes或者no"
		read y_or_n
		case "$y_or_n" in
			y*) rm yiyi
			    echo "已经删除yiyi";;
			n*) echo "no";;
		esac
	else
		touch yiyi
	fi
#创建表	
	echo 请输入i的值
	read i
	j=0

	while [ "$j" -lt "$i" ]; do
		echo -e $j >> yiyi
		j=`expr $j + 1`
	done
}

case "$1" in
	IPv4) echo "IPv4 table"
	      ipv4;;
	IPv6) echo "IPv6 table"
	      ipv6;;
	* | h* | H*)    usage;;
esac

exit 0
